package com.ldk.framework.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ldk.framework.entity.CountBean;
import com.ldk.framework.entity.Vehicle;

import org.apache.http.util.TextUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.ldk.base.framework.dao.impl.BaseDAO;
import com.ldk.framework.dao.IDriverDAO;
import com.ldk.framework.entity.Driver;
import com.ldk.framework.entity.DriverBindCar;
import com.ldk.framework.entity.LinkDriver;

/**
* DriverDAO接口实现类
* @author xiangwl
*/
@Repository
public class DriverDAO extends BaseDAO<Driver> implements IDriverDAO {

	@Override
	public List<Driver> searchByDistance(String dbKey, Driver query, double distance, Pageable pageable) {
		Assert.notNull(distance);
		Assert.notNull(query);
		try{
			Map<String, Object> parms = getPageParms(query, pageable);
			parms.put("distance", distance);
			
			SqlSession sqlSession = getSqlSession(dbKey);
			
			return sqlSession.selectList(getSqlName("searchByDistance"), parms);
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public LinkDriver getLinkDriver(String dbKey, String openId) {
		Assert.notNull(openId);
		try{
			Map<String, Object> parms = new HashMap<String, Object>();
			parms.put("openId", openId);
			
			SqlSession sqlSession = getSqlSession(dbKey);
			
			LinkDriver linkDriver = sqlSession.selectOne(getSqlName("getLinkDriver"), parms);
			
			return linkDriver;
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<DriverBindCar> findBindDriverList(String dbKey) {
		try{

			SqlSession sqlSession = getSqlSession(dbKey);

			return sqlSession.selectList(getSqlName("findBindDriverList"));
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public List<DriverBindCar> findVehicleList(String dbKey) {
		try{
			SqlSession sqlSession = getSqlSession(dbKey);

			return sqlSession.selectList(getSqlName("findVehicleList"));
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public CountBean countAnalysis(String dbKey, String companyId) {
		try{
			SqlSession sqlSession = getSqlSession(dbKey);
			if(!TextUtils.isEmpty(companyId)){
				Map<String, Object> parms = new HashMap<String, Object>();
				parms.put("companyId", companyId);
				return sqlSession.selectOne(getSqlName("countAnalysisByCompanyId"), parms);
			}else {
				return sqlSession.selectOne(getSqlName("countAnalysis"));
			}
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}


}
